<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/2/3
 * Time: 13:03
 */
defined('IN_TS') or die('Access Denied.');

$js = intval($_GET['js']);

$userid = intval($TS_USER['userid']);
$orderid = intval($_REQUEST['orderid']);
$strOrder = $new['gift']->find('gift_users',array(
    'orderid'=>$orderid,
));
if($strOrder==''){
    tsNotice('订单不存在！');
}

$strGift = $new['gift']->find('gift',array('giftid'=>$strOrder['giftid']));

$price = $strOrder['money'];

$forpayid = intval($_REQUEST['forpayid']);

if($forpayid) {
    //已有单号
    $strPayUser =aac('pay')->find('pay_user', array('forpayid' => $forpayid));
    if ($strPayUser['trade_status'] == "TRADE_SUCCESS") {
    tsNotice('该单已支付过！', '返回>>', tsUrl('pay'));
    }

    $price = $strPayUser['price'];
    $types = trim($strPayUser['types']);
}else{
    //生成订单


        $arrData = array(
            'userid'=>$userid,
            'types'=>'weixin',
            'price'=>$price,
            'orderid'=>$orderid,
            'addtime'=>time(),
        );
        $payid = $new['gift']->create('pay_user',$arrData);
        $forpayid = $payid + $arrData['addtime'];
        $new['gift']->update('pay_user',array(
            'payid'=>$payid
        ),array(
            'forpayid'=>$forpayid
        ));


}

/*
 * 微信支付
 */


    $price2 = str_replace('.','',$price);
    $price2 = $price2*10;

    #echo $price2;exit;

    require "WxpayAPI_php_v3/lib/WxPay.Api.php";
    require "WxpayAPI_php_v3/example/WxPay.JsApiPay.php";
    require 'WxpayAPI_php_v3/example/log.php';
    /*
    $notify = new NativePay();
    $input = new WxPayUnifiedOrder();
    $input->SetBody($_SERVER['HTTP_HOST'].$forpayid.'userid'.$userid.'price'.$price.'articleid'.$articleid);
    $input->SetAttach($forpayid.'|'.$articleid);
    $input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));
    $input->SetTotal_fee($price2);
    $input->SetTime_start(date("YmdHis"));
    $input->SetTime_expire(date("YmdHis", time() + 6000));
    $input->SetGoods_tag("test");
    $input->SetNotify_url(SITE_URL."WxpayAPI_php_v3/example/notify.php");
    $input->SetTrade_type("NATIVE");
    $input->SetProduct_id($forpayid);
    $result = $notify->GetPayUrl($input);

    //print_r($result);

    $url2 = $result["code_url"];
    */


    //打印输出数组信息
    function printf_info($data)
    {
        foreach($data as $key=>$value){
            echo "<font color='#00ff55;'>$key</font> : $value <br/>";
        }
    }

//①、获取用户openid
    $tools = new JsApiPay();
    $openId = $tools->GetOpenid();

    #echo $openId;exit;

    if($openId){

        //②、统一下单
        $input = new WxPayUnifiedOrder();
        #$input->SetBody($_SERVER['HTTP_HOST'].$forpayid.'userid'.$userid.'price'.$price.'articleid'.$articleid);
        $input->SetBody('支付《'.$strGift['title'].'》');
        $input->SetAttach($forpayid.'|'.$orderid);
        $input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));
        $input->SetTotal_fee($price2);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 3600));
        $input->SetGoods_tag("test");
        $input->SetNotify_url(SITE_URL."WxpayAPI_php_v3/example/notify.php");
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $order = WxPayApi::unifiedOrder($input);


        //echo '<font color="#f00"><b>统一下单支付单信息</b></font><br/>';
        //printf_info($order);
        $jsApiParameters = $tools->GetJsApiParameters($order);

//获取共享收货地址js函数参数
        $editAddress = $tools->GetEditAddressParameters();






        $title = '微信支付';
        include template('weixin');

    }else{
        tsNotice('服务器网络不给力，获取微信支付失败！');
    }